import { useMemberStore } from "../stores"
const store=useMemberStore()
export default {
	ws: {},
	setWs: function(newWs) {
		// this.ws = newWs
	},

	start() { // 发送数据
	// 
		const data1 = {
			ReqType: "bound",
			ReqUser: store.profile?.openId,
			ReqData: "绑定",
			CheckData: store.profile?.openId,
			RcvUsers: ['wx']
		}
		this.ws.send({
			//向服务器发送消息
			data: JSON.stringify(data1),
		})
		this.timeoutObj = setInterval(() => {
			if (this.ws && this.ws.readyState == 1) {
				this.ws.send({
					data: JSON.stringify(data1),
				})
			}
		}, 10 * 2000) //十秒发一次
	},
	localSocket() { //连接ws，根据连接服务器是否需要参数设置该方法是否需要接收参数

		this.ws = wx.connectSocket({
			url: 'wss://api.victorick.com:11011',
			fail: function(err) {
				console.log(err)
			}
		})
        console.log(this.ws,'67898');
		this.ws.onOpen((res) => {
			this.start()
		})
		this.ws.onclose = function() {
			// 关闭 websocket
			console.log("连接已关闭...");
			//断线重新连接
			setTimeout(() => {
				this.localSocket();
			}, 2000);
		};
	},
}